const { values } = require("mobx-miniprogram");
const { apiUrl } = require("../../app");
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import { store } from "../../store/store";
import {
  getCommentLikeStatus,
  likeComment,
  unlikeComment,
} from "../../utils/apis";

// components/review-view/review-view.js
Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: {
    store,
    fields: {
      authorization: "authorization",
    },
  },
  /**
   * 组件的属性列表
   */
  properties: {
    review: Object,
    showObjName: {
      type: Boolean,
      values: false,
    },
    type: String,
  },
  lifetimes: {
    attached() {
      getCommentLikeStatus(this.data.review.id).then((res) => {
        this.setData({
          count: res.data.data.count,
          isLike: res.data.data.did_i_like,
        });
      });
      if (this.data.review.edges.children) {
        this.setData({
          child: this.data.review.edges.children.length,
        });
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    child: 0,
    count: 0,
    isLike: false,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    previewImage(event) {
      const index = event.currentTarget.dataset.index;
      var photo = this.properties.review.imagesList;
      wx.previewImage({
        current: photo[index],
        urls: photo,
      });
    },
    unLike() {
      unlikeComment(this.data.review.id);
      const count = this.data.count;
      this.setData({
        isLike: false,
        count: count - 1,
      });
    },
    giveLike() {
      likeComment(this.data.review.id);
      const count = this.data.count;
      this.setData({
        isLike: true,
        count: count + 1,
      });
    },
    showComments(event) {
      const review = this.data.review;
      this.triggerEvent("customEvent", review);
    },
  },
});
